/**
 * @file factory_mode_aging.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_FACTORYMODE_AGING_MODE != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static void factorymode_agingtest_prepare_destory(lv_obj_t * activity_obj);
static lv_aging_obj_ext_t * agingtest_get_ext(void);
static void agingtest_clean_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void agingtest_item_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static lv_obj_t * agingtest_item_create(lv_obj_t * par);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * factorymode_agingtest_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_FACTORY_MODE_AGINGTEST;
        activity_ext.create = factorymode_agingtest_create;
        activity_ext.prepare_destory = factorymode_agingtest_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_aging_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_aging_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    lv_obj_t * btn_clean = lv_btn_create(obj, NULL);
    lv_obj_t * label_clean = lv_label_create(btn_clean, NULL);
    lv_obj_add_style(label_clean, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_clean, WATCH_TEXT_ID_TEST_REPORT_CLEAN);
    lv_obj_set_size(btn_clean, LV_HOR_RES * 2 / 3, 30);
    lv_obj_align(btn_clean, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
    lv_obj_set_event_cb(btn_clean, agingtest_clean_btn_event_cb);

    lv_obj_t * cont = agingtest_item_create(obj);
    lv_obj_align(cont, btn_clean, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

    lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
    ext_fac->testmode = TEST_AGING;
    ext_fac->speaker_volume = 10;
    ext_fac->timer = NULL;

    return obj;
}

void agingtest_update(uint8_t num)
{
    lv_aging_obj_ext_t * ext = agingtest_get_ext();
    lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
    if(ext_fac->agingtest_res[num] == TEST_FAILED) {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->red_st));
    } else if(ext_fac->agingtest_res[num] == TEST_SUCCESS) {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->green_st));
    } else {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->white_st));
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void factorymode_agingtest_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    autotest_hal_stop();
}

static lv_aging_obj_ext_t * agingtest_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_FACTORY_MODE_AGINGTEST);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_aging_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void agingtest_clean_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        uint8_t i = 0;
        factorymode_clear_agingtest_res();
        for(i = 0; i < AGINGTEST_ITEM_NUM; i++) {
            agingtest_update(i);
        }
    }
}

static void agingtest_item_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        uint8_t num = lv_obj_get_user_data(btn).user_num;
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_FACTORY_MODE_AGINGTEST);
        lv_obj_t * obj = lv_watch_obj_create(activity_obj);
        LV_ASSERT_MEM(obj);
        lv_factory_obj_ext_t * ext = factorymode_get_ext();
        switch(num) {
        case 0:
            autotest_speaker_create(obj);
            ext->module = AUTOTEST_SPEAKER;
            break;
        case 1:
            autotest_lcdcolor_aging_create(obj);
            ext->module = AUTOTEST_LCD_COLOR;
            break;
        case 2:
            autotest_camera_create(obj);
            ext->module = AUTOTEST_CAMERA;
            break;
        case 3:
            autotest_call_create(obj);
            ext->module = AUTOTEST_CALL;
            break;
        default:
            break;
        }
    }
}

static lv_obj_t * agingtest_item_create(lv_obj_t * par)
{
    const watchLangTxtId_t txtId[4] = {
        WATCH_TEXT_ID_SPEAKER_TEST,
        WATCH_TEXT_ID_LCDTEST,
        WATCH_TEXT_ID_CAMERA_TEST,
        WATCH_TEXT_ID_CALL,
    };

    lv_obj_t * cont = lv_cont_create(par, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 10, 150);
    lv_cont_set_layout(cont, LV_LAYOUT_GRID);
    lv_obj_set_click(cont, false);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);

    lv_aging_obj_ext_t * ext = agingtest_get_ext();
    lv_obj_t * label[AGINGTEST_ITEM_NUM];
    uint8_t i = 0;
    for(i = 0; i < AGINGTEST_ITEM_NUM; i++) {
        ext->btn[i] = lv_btn_create(cont, NULL);
        lv_page_glue_obj(ext->btn[i], true);
        label[i] = lv_label_create(ext->btn[i], NULL);
        lv_label_set_align(label[i], LV_LABEL_ALIGN_CENTER);
        lv_label_set_text_id(label[i], txtId[i]);
        lv_obj_set_click(ext->btn[i], true);
        lv_obj_set_click(label[i], false);
        lv_obj_set_size(ext->btn[i], (LV_HOR_RES - 10 - 2 * 2) / 2, 50);
        lv_obj_set_event_cb(ext->btn[i], agingtest_item_btn_event_cb);
        lv_watch_set_free_num(ext->btn[i], i);
        lv_obj_add_protect(ext->btn[i], LV_PROTECT_PRESS_LOST);
        agingtest_update(i);
    }

    return cont;
}

#endif /*USE_LV_WATCH_FACTORY_MODE*/
